#include "hud_item_cannon.h"

extern int G_W, G_H;

HUD_ITEM_CANNON::HUD_ITEM_CANNON(CASH *cash):QObject(), QGraphicsItemGroup()
{

    this->cash=cash;

    init();
    init_decor();

    tl_increment = new QTimeLine(1900, this);
    tl_increment->setFrameRange(0, 4);


    connect(tl_increment, SIGNAL(frameChanged(int)), this, SLOT(slot_tl_increment()));

}

QPainterPath HUD_ITEM_CANNON::shape() const
{
    return QPainterPath();
}

void HUD_ITEM_CANNON::init()
{
    tl_anim_items = new QTimeLine(800, this);

    lst_anims = new QList<QGraphicsItemAnimation*>();

    int tmp_w = 0;
    points = 0;

    for(int i=0;i<4;i++){
        GRAPHICS_PIXMAP_ITEM *item = new GRAPHICS_PIXMAP_ITEM(*cash->cash_pixmap->value("Res/HUD/cannon/item.svg"), this);
        QGraphicsItemAnimation *anim = new QGraphicsItemAnimation(this);

        lst_anims->append(anim);
        anim->setItem(item);

        item->setPos(- G_W, - G_H);
        anim->setPosAt(0, QPointF((70 + tmp_w) + (item->boundingRect().width()*2), G_H - item->boundingRect().height() - 30));
        anim->setPosAt((i + 1)*0.1, QPointF(70 + tmp_w, G_H - item->boundingRect().height() - 30));

        anim->setTimeLine(tl_anim_items);

        tmp_w += item->boundingRect().width();
        points++;

    }

    hud_shadow = new QGraphicsDropShadowEffect(this);
    hud_shadow->setBlurRadius(7);
    hud_shadow->setColor(QColor("#2d5cff"));
    hud_shadow->setOffset(0);

    this->setGraphicsEffect(hud_shadow);
}

void HUD_ITEM_CANNON::init_decor()
{
    tl_anim_decor = new QTimeLine(500);
    tl_anim_decor->setEasingCurve(QEasingCurve::Linear);

    cannon_icon = new GRAPHICS_PIXMAP_ITEM(*cash->cash_pixmap->value("Res/HUD/cannon/icon.svg"), this);
    cannon_border = new GRAPHICS_PIXMAP_ITEM(*cash->cash_pixmap->value("Res/HUD/cannon/border.svg"), this);

    cannon_icon->setPos(- G_W, - G_H);
    cannon_border->setPos( - G_W, - G_H);

    QGraphicsItemAnimation *anim_decor = new QGraphicsItemAnimation(this);

    anim_decor->setItem(cannon_icon);
    anim_decor->setTimeLine(tl_anim_decor);

    anim_decor->setPosAt(0, QPointF(- cannon_icon->boundingRect().width(), G_H - cannon_icon->boundingRect().height() - 10));
    anim_decor->setPosAt(0.5, QPointF(14, G_H - cannon_icon->boundingRect().height() - 10));

    anim_decor = new QGraphicsItemAnimation(this);

    anim_decor->setItem(cannon_border);
    anim_decor->setTimeLine(tl_anim_decor);

    anim_decor->setPosAt(0, QPointF(74, G_H));
    anim_decor->setPosAt(0.5, QPointF(74, G_H - cannon_border->boundingRect().height() - 20));
}

void HUD_ITEM_CANNON::set_points(int value)
{
    this->points = value;
    value--;

    for(int i=0;i<lst_anims->count();i++){
        if(i > value && i < lst_anims->count())
            lst_anims->at(i)->item()->setVisible(false);

        if(i >= 0 && i <= value)
            lst_anims->at(i)->item()->setVisible(true);
    }
}

int HUD_ITEM_CANNON::get_points()
{
    return this->points;
}

HUD_ITEM_CANNON::~HUD_ITEM_CANNON(){}

void HUD_ITEM_CANNON::show_hud()
{
    tl_anim_decor->stop();
    tl_anim_decor->setDirection(QTimeLine::Forward);
    tl_anim_decor->start();

    connect(tl_anim_items, SIGNAL(valueChanged(qreal)), this, SLOT(slot_show_items()));

    tl_anim_items->stop();
    tl_anim_items->setDirection(QTimeLine::Forward);
    tl_anim_items->start();
}

void HUD_ITEM_CANNON::hide_hud()
{
    tl_anim_decor->stop();
    tl_anim_decor->setDirection(QTimeLine::Backward);
    tl_anim_decor->start();

    tl_anim_items->stop();
    tl_anim_items->setDirection(QTimeLine::Backward);
    tl_anim_items->start();

    connect(tl_anim_items, SIGNAL(finished()), this, SLOT(slot_hide_items()));
}

void HUD_ITEM_CANNON::slot_hide_items()
{
    if(tl_anim_items->direction() == QTimeLine::Backward){
        foreach (QGraphicsItemAnimation* anim, *lst_anims) {
           anim->item()->setVisible(false);
        }
    }

    disconnect(tl_anim_items, SIGNAL(finished()), this, SLOT(slot_hide_items()));
}

void HUD_ITEM_CANNON::slot_show_items()
{
    if(tl_anim_items->direction() == QTimeLine::Forward){
        int cnt = 0;

        foreach (QGraphicsItemAnimation* anim, *lst_anims) {
           anim->item()->setVisible(true);
           cnt++;

           if(cnt == points)
               break;
        }
    }

    disconnect(tl_anim_items, SIGNAL(finished()), this, SLOT(slot_show_items()));
}

void HUD_ITEM_CANNON::slot_fire()
{
    tl_increment->start();
    set_points(0);
}

void HUD_ITEM_CANNON::slot_tl_increment()
{
    set_points(get_points() + 1);
}

